<%@page import="org.apache.commons.io.FileUtils"%>
<%@ page import="uniLearn.control.*"%>
<%@ page import="uniLearn.data.*"%>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Hashtable" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="java.text.SimpleDateFormat"%>
<%--
<%@ page import="java.net.URLEncoder" %>
 --%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true" %>
<%!
	//Define Error Message
	static final String ERR_LOGIN_FAIL = "Incorrect name or password, Please login again.";

	//Define upload file size
	static final long MAX_FILE_SIZE = FileUtils.ONE_MB * 4;
	static final long MAX_REQUEST_SIZE = FileUtils.ONE_MB * 4 + FileUtils.ONE_KB;
	
	//Define Session attribute name
	static final String SESSION_LOGIN_USER = "loginUser";
	static final String SESSION_SELECT_UNIT = "selectUnit";
	
	//Define reauest parameter name
	static final String PAR_LOGIN_NAME = "login_name";
	static final String PAR_PASSWORD = "password";
	static final String PAR_UNIT_ID = "unit_id";
	static final String PAR_ASSIGNMENT_ID = "ac_id";
	static final String PAR_SUBMIT_BY = "submit_by";
	static final String PAR_MARK = "mark_";
	static final String PAR_FEEDBACK = "feedback_"; 
	static final String PAR_TOTAL_MARK = "total_mark";
	static final String PAR_UPLOAD_FILE = "upload_file";
	
	//Define Redirect URL
	//main page of user
	static final String URL_STUDENT = "Student/Student_index.jsp";
	static final String URL_STAFF = "Staff/Staff_index.jsp";
	static final String URL_ADMIN = "Admin/admin_index.jsp";
	//In all folders folder
	static final String URL_HOMEPAGE = "../index.html";
	static final String URL_DOWNLOAD_PROCESS = "../download_process.jsp";
	static final String URL_DOWNLOAD_ASSIGNMENT = "download_assignment.jsp";
	static final String URL_VIEW_DETAIL_RESULT = "view_detail_result.jsp";
	//In student folder
	static final String URL_SELECT_ASSIGNMENT = "f_assignment_select.jsp";
	static final String URL_SELECT_UNIT = "f_unit_select.jsp";
	static final String URL_UPLOAD_ASSIGNMENT = "f_upload_submission.jsp";
	static final String URL_CONFIRM_SUBMISSION = "f_confirm_submission.jsp";
	//In staff folder
	static final String URL_VIEW_ASSIGNMENT = "f_staff_view_assignment.jsp";
	static final String URL_UPDATE_ASSIGNMENT = "create_assignment_collector.html";
	static final String URL_VIEW_SUBMITED = "f_staff_view_submit_assignment.jsp";
	static final String URL_MARK_SUBMIT = "f_staff_mark_submit_assignment.jsp";
	
	//shared function
	static boolean loginedChecking(HttpSession session, UsersType reqType) {
		Object loginUser = session.getAttribute(SESSION_LOGIN_USER);
		if(!objectChecking(loginUser, Users.class))
			return false;
		
		return ((Users)loginUser).getUserType() == reqType;
	}
	
	static boolean selectUnitCheck(HttpSession session) {
		return objectChecking(session.getAttribute(SESSION_SELECT_UNIT), Units.class);
	}
	
	static boolean objectChecking(Object sessionValue, Class targetClass) {
		if(sessionValue == null)
			return false;
		
		return targetClass.isInstance(sessionValue);
	}
	
	static boolean isEmptyString(String str) {
		return (str == null || str.isEmpty());
	}
	
	static String addParmeterToUrl(String url, String name, String value) throws Exception {
		String newURL = url + "?"+ name+ "="+ value;
		//return URLEncoder.encode(newURL,"UTF-8");
		return newURL;
	}
	
	static String addParmeterToUrl(String url, Hashtable<String, String> parTable) throws Exception {
		StringBuilder newURL = new StringBuilder(url);
		if(!parTable.isEmpty()) {
			newURL.append("?");
			java.util.Enumeration<String> keys = parTable.keys();
			String key = keys.nextElement();
			//add the first key & values to url
			newURL.append(key);
			newURL.append("=");
			newURL.append(parTable.get(key));
			//add the others
			while(keys.hasMoreElements()) {
				key = keys.nextElement();
				newURL.append("&");
				newURL.append(key);
				newURL.append("=");
				newURL.append(parTable.get(key));	 
			}
		}
		
		//return URLEncoder.encode(newURL.toString(),"UTF-8");
		return newURL.toString();
	}
	
	static String formatDateTime(Date datetime) {
		return (datetime != null) ? new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(datetime) : "";
	}
	
	static String muiliteLineString(String str) {
		return str.replaceAll("\r\n", "<br />");
	}
	
	/* static org.apache.commons.fileupload.ProgressListener createListener() {
		return new org.apache.commons.fileupload.ProgressListener() {
			private long megaBytes = -1;
			public void update(long pBytesRead, long pContentLength, int pItems) {
				long mBytes = pBytesRead / FileUtils.ONE_MB;
				if(mBytes == this.megaBytes) 
					return;
				
				
			}
		};
	} */
%>